<?php

class SNSAdminAction extends Action{

   function _initialize() {
    header('content-Type=text/html;charset=utf-8');
    $this->_dev = C("DEV_FLAG");
    if (ACTION_NAME != "login") {
      $this->checkLogin();
    }
  }

  public function checkLogin() {
    if ($_SESSION['user_name'] != "ehs" && $_SESSION['password'] != "ehscn") {
      if($this->isAjax)$this->redirect('SNSAdmin/login','',2,'您还没有登陆，请重新登陆，页面跳转中~');
	  else $this->ajaxReturn('0','请先登录后操作','0');
    }
  }

   public function index(){//显示社区文章列表
       // error_reporting(E_ALL);
        $db=M('APwColonysArticle');

       // $db->create();
       //$list=$db->query('select * from pw_diary');
       $list=$db->select();
       //echo 'l=';print_r($list);
        import("@.ORG.Page"); //导入分页类
        $count =count($list);    //计算总数
        $p = new Page ( $count,20);
        $list=$db->order('did desc')->limit("$p->firstRow,$p->listRows")->select();//query("select * from pw_diary limit {$p->firstRow},{$p->listRows}");//limit($p->firstRow.','.$p->listRows)->order('id desc')->findAll();
        $page = $p->show ();
        $this->assign( "page", $page );
        $this->assign ( "list", $list );
        //$this->display();
        //print_r($list);
        $this->assign('list',$list);
        //print_r($list);
        $this->display();
    }
	
	
	public function memberList(){//显示社区文章列表
       // error_reporting(E_ALL);
        $db=M('PwMembers');
       $list=$db->select();
       //echo 'l=';print_r($list);
        import("@.ORG.Page"); //导入分页类
        $count =count($list);    //计算总数
        $p = new Page ( $count,20);
        $list=$db->limit("$p->firstRow,$p->listRows")->select();
        $nav= $p->show ();
		
		foreach($list as &$li){
		  $icons = explode('|',$li['icon']);
           $li['icon']= $icons[1]>0 ? "http://ehs.cn/pw8/attachment/upload/".$icons[0] : "http://ehs.cn/pw8/images/face/none.gif";
		}		
		//dump($list);
        $this->assign("page", $page);
		$this->assign("nav", $nav);
        $this->assign ("list", $list);
		$this->assign ("count", count($list));
        //$this->display();
        //print_r($list);

		//$this->
        //print_r($list);
        $this->display('member_list');
    }

   /*返回推荐内容列表*/
   public function recommendAjax(){
     $id=empty($_POST['id'])?'0':$_POST['id'];
	 $page=empty($_POST['page'])?'1':$_POST['page'];
     $type=empty($_POST['type'])?'1':$_POST['type'];
	 $kw=empty($_POST['keyword'])?'':$_POST['keyword'];
	 $tables=array(1=>'APwColonysArticle',2=>'PwMembers',3=>'PwDiary');
	 $fields=array(1=>'title',2=>'username',3=>'subject');
	 $order=array(1=>'create_time DESC',2=>'username ASC',3=>'postdate DESC');
	 
	 
	 $m=M($tables[$type]);
	  if($kw!=''){
	   $map[$fields[$type]]=array('like',"%{$kw}%");
	   $count=$m->where($map)->count();
	  }else
	  $count=$m->count();
	  
	  //exit();
       //echo 'l=';print_r($list);
        import("@.ORG.pageAjax"); //导入分页类
        //$count =count($count);    //计算总数
		
        $p = new pageAjax (array('total'=>$count,'pagesize'=>20,'ajax'=>'jumpTo','page'=>$page));
		if($kw!=''){
		  $map[$fields[$type]]=array('like',"%{$kw}%");
          $list=$m->where($map)->limit("$p->firstRow,$p->listRows")->order($order[$type])->select();
		}else
		  $list=$m->limit("$p->firstRow,$p->listRows")->order($order[$type])->select();
        $nav= $p->show ();
		
		alert($m->getLastSQL());
		
		if($type==2){
		   foreach($list as &$li){
		    $icons = explode('|',$li['icon']);
            $li['icon']= $icons[1]>0 ? "http://ehs.cn/pw8/attachment/upload/".$icons[0] : "http://ehs.cn/pw8/images/face/none.gif";
		   }		 
		}
		$this->assign("nav",$nav);
        $this->assign ("list",$list);
		$this->assign ("type",$type);
		$this->assign ("id", $id);
		$this->assign ("count", count($list));
        //$this->display();
        //print_r($list);
        //echo $m->getLastSQL();
		//$this->
        //print_r($list);
        $this->display('recommend_ajax');
          
   }


    /*  登陆  */
  public function login() {
    if ($_POST['user_name'] == "ehs" && $_POST['password'] == "ehscn") {
      $_SESSION['user_name'] = "ehs";
      $_SESSION['password'] = "ehscn";
      $this->redirect('SNSAdmin/index', '', 2, "登陆成功，页面跳转中~~~");
    }else {
      $this->display();
    }
  }

  public function recommendZone(){
     //it('1111');
	 import("@.ORG.Page"); //导入分页类
     $q=M('ARecommendPart');     
     $list=$q->select();
     //echo $sql;
	 $pager=new Page(count($list),20);
	 $list=$q->limit("$pager->firstRow,$pager->listRows")->select();
	 $nav=$pager->show();
	 //dump($list);//exit();
	 
     $this->assign('list',$list);
	 $this->assign('count',count($list));
	 $this->assign('nav',$nav);
     $this->display('recommend_zone_list');
  }
  
  /*增加、编辑推荐位*/
  public function recommendZoneForm(){
    
	$this->display('recommend_zone_form');
  
  }
  
  
  /*推荐位提交*/
  public function recommendZoneFormSubmit(){
     if(empty($_POST['name'])){
	    $this->error('名称不能为空');
		exit();
	 }
	 
	 $id=empty($_POST['id'])?'0':$_POST['id'];
	 if(!$id){//增加操作
	   $m=M('ARecommendPart');
	   $m->create();
	   if($m->add()){
	     //echo $m->getLastSQL();exit();
	     $this->assign('jumpUrl','__URL__/recommendZone');
	     $this->success('添加成功');
	    }
		else
	     $this->error('添加失败');	   
	  } else{//更新操作
	      $m=M('ARecommendPart');
		  $m->save($_POST);	
		  $this->assign('jumpUrl','__URL__/recommendZone');
		  $this->success('更新成功');   
	  }		  
  }
  
  /*编辑*/
  public function editRecommendZone(){
     $id=empty($_GET['id'])?0:$_GET['id'];
	 $m=M('ARecommendPart');
	 $item=$m->find($id);
	 $this->assign('item',$item);
	 $this->assign('id',$id);
	 $this->display('recommend_zone_form');	 
  }
  
  
  /*推荐位删除*/
  public function delRecommendZone(){
     $ids=empty($_GET['id'])?array(0):array($_GET['id']);
	  if($ids==array(0))
	    $ids=empty($_POST['dids'])?array(0):$_POST['dids'];
    // print_r($ids);
	  foreach($ids as $id){
        $db=M('ARecommendPart');
        $db->delete($id);
	  }
	  
      $this->assign('jumpUrl','__URL__/recommendZone');
	  $this->success('成功删除'.count($ids).'条记录'); 
  }
  
  //推荐区域，推荐内容管理
  public function  recommendList(){
    !empty($_GET['id'])||$this->error('参数错误');
	
	$id=$_GET['id'];
	$m=M('ARecommend');
	$list=$m->where("part_id={$id}")->order('sort ASC')->select();
	
	foreach($list as &$li){
	  if($li['type']==1){
	    $m=M('APwColonysArticle');
		$item=$m->find($li['type_id']);
		$li['title']="标题：<a href='/main/public.php?s=/circle/article/cyid/{$item['cyid']}/id/{$item['id']}' target='_blank'>".$item['title'].'</a>';
	  }elseif($li['type']==2){
	    $m=M('PwMembers');
		$item=$m->find($li['type_id']);
		$li['title']="用户：<a href='/main/public.php?s=/column/index/uid/{$item['uid']}' target='_blank'>".$item['username'].'</a>';
	  }	else{
	    $m=M('PwDiary');
		$item=$m->find($li['type_id']);
		///main/public.php?s=/column/diaryView/uid/6177/did/2129
		$li['title']="用户：<a href='/main/public.php?s=/column/diaryView/uid/{$item['uid']}/did/{$item['did']}' target='_blank'>".$item['subject'].'</a>';	  
	  }
	  
	}
	
	$this->assign('list',$list);
	$this->assign('id',$id);
	$this->assign('count',count($list));
	$this->display('recommend_list');		
  }
  
  //推荐区域，推荐内容增加
  public function  recommendForm(){
    !empty($_GET['id'])||$this->error('参数错误');
	
	$id=$_GET['id'];
	$m=M('ARecommendPart');
    $item=$m->find($id);
	$this->assign('item',$item);
	$this->assign('id',$id);
	$this->display('recommend_form');
		
  }


  /*推荐内容排序*/
  public function setRecommendOrder(){
      $ids=empty($_POST['ids'])?array():$_POST['ids'];
      $order=empty($_POST['order'])?array():$_POST['order'];
     // print_r($order);
      foreach ($ids as $k=>$id){
         $db=M('ARecommend');
         //$db->query("update a_pw_recommend_diary set sort={$order[$k]} where id={$id}");
         //echo "update a_pw_recommend_diary set sort={$order[$k]} where id={$id}";
        $data['sort']=$order[$k];
        $db->where("id={$id}")->save($data);
      }
	  $this->success('排序成功');
  }



  /*删除推荐内容*/
  public function delRecommend(){
      $ids=empty($_GET['id'])?array(0):array($_GET['id']);
	  if($ids==array(0))
	    $ids=empty($_POST['dids'])?array(0):$_POST['dids'];
    // print_r($ids);
	  foreach($ids as $id){
        $db=M('ARecommend');
        $db->delete($id);
	  }
      $this->success('成功删除'.count($ids).'条记录');
  }

    
   /*增加推荐内容*/
   public function toRecommend(){
      
	  $ids=empty($_GET['ids'])?array(0):array($_GET['ids']);
	  if($ids==array(0))
	    $ids=empty($_POST['ids'])?array(0):$_POST['ids'];
	  
	  $type=$_REQUEST['type'];
	  $part_id=$_REQUEST['part_id'];
      
	  //dump($_REQUEST);
      $db=M('ARecommend');//APwRecommendDiary
    // echo 'aa'; print_r($ids);
	  //$db->create(); 
	   //dump($ids);
	  if(!is_array($ids))
	   $ids=array($ids);
	
      foreach($ids as $id){
        //$db->query("insert into a_pw_recommend_diary(diary_id,sort) values({$id},1)");
        $data['type']=$type;
		$data['type_id']=$id;
        $data['sort']=1;
		$data['part_id']=$part_id;
        $db->add($data);
      }
	  //exit($db->getLastSQL());
      $this->success('成功添加'.count($ids).'条推荐记录');
   }
   
   
   public function huodong_member(){
      if(file_exists('PublicFunction/upload/huodong.txt')){
	     $this->assign('flag','1');   
	  }else{
	  	 $this->assign('flag','0');   
	  }  
	  $this->display(); 
   }
   
   public function empty_memberList(){
      if(file_exists('PublicFunction/upload/huodong.txt'))
	    file_put_contents('PublicFunction/upload/huodong.txt','');
	  $this->ajaxReturn('0','操作成功','1');
   }
   

 }

?>